package com.test.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.dao.CategoryDao;
import com.test.dao.GoodsDao;
import com.test.entity.Category;
import com.test.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    GoodsDao goodsDao;

    @Override
    public List<Category> queryAllCategory() {
        return categoryDao.queryAllCategory();
    }

    @Override
    public PageInfo<Category> queryAllByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Category> category = categoryDao.queryAllCategory();
        PageInfo<Category> pageInfo = new PageInfo<>(category);
        return pageInfo;
    }

    @Transactional
    @Override
    public int deleteById(Integer id) {
        // 删除该类别下所有的商品
        goodsDao.deleteByCategoryId(id);
        // 删除类别
        int num = categoryDao.deleteById(id);
        // 删除订单详请
        // 更新订单详请

        return num;
    }


    @Override
    public int update(Category category) {
        return categoryDao.update(category);
    }

    @Override
    public int insert(String name) {
        return categoryDao.insert(name);
    }
}
